within ThreeD_MBS_Dynamics.Examples.Rattleback;

model Case_of_Kane_Rattleback
  extends RigidBody;
  outer Real[3] Gravity;
  // Ellipsoid semi-diameters
  outer Real a1;
  outer Real b1;
  outer Real c1;
  outer Real h;
  outer SI.Angle delta;
  parameter Real R[3, 3]=[cos(delta), 0, sin(delta); 0, 1, 0; -sin(delta), 0,
       cos(delta)];
  VisualShape Body(
    r0={0,h,-c1},
    Shape="sphere",
    LengthDirection={0,0,1},
    WidthDirection={1,0,0},
    Length=2*c1,
    Width=2*a1,
    Height=2*b1,
    Material={1,0,0,1});
  //  Real p1[3];
  Real E;
  Real K1;
  Real K2;
  Real P;
  Real Mod_q2;
  Real gamma(start=0);
  Real KaneGamDeg;
  WrenchPort InPortRoll annotation (extent=[-10, 80; 10, 100]);
equation
  F = m*Gravity + InPortRoll.F;
  M = InPortRoll.M + cross(InPortRoll.P - r, InPortRoll.F);
  Body.S = T*R;
  Body.r = r;
  //  Body.r = {0,r[2],r[3]};
  //  p1 = r + T*{2,0,0};
  K1 = 0.5*m*v*v;
  K2 = 0.5*omega*I*omega;
  P = -m*r*Gravity;
  E = K1 + K2 + P;
  Mod_q2 = q[1]^2 + q[2]^2 + q[3]^2 + q[4]^2;
  der(gamma) = OutPort.omega[2];
  KaneGamDeg = -gamma*180/Modelica.Constants.pi;
  annotation (Diagram);
end Case_of_Kane_Rattleback;
